
<template>
  <div style="width:98%;margin: 0 auto;">
    <div style="width:100%;margin-top:20px;">
      <el-form :inline="true" size="medium">
        <el-form-item label="角色名称：">
          <el-input
            v-model="queryParams.roleName"
            placeholder="请输入角色名称"
            clearable
            maxlength="50"
          />
        </el-form-item>
        <el-form-item label="状态：">
          <el-select v-model="queryParams.status" placeholder="角色状态" clearable size="medium">
            <el-option label="正常" :value="0" />
            <el-option label="禁用" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getDataList()">搜索</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="handleAdd()">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        border
        size="medium"
        align="center"
        tooltip-effect="dark"
        style="width: 100%;margin-top:-5px;"
      >
        <el-table-column
          type="index"
          label="序号"
          align="center"
          width="70"
        />
        <el-table-column
          label="角色名称"
          align="center"
          prop="roleName"
          show-overflow-tooltip
        />
        <el-table-column
          label="角色标识"
          align="center"
          prop="roleIdentifier"
          show-overflow-tooltip
        />
        <el-table-column
          prop="remark"
          label="备注"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column
          prop="status"
          label="状态"
          align="center"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" :content="scope.row.status === 1?'点击启用':'点击禁用'" placement="top">
              <el-button
                size="medium"
                :type="scope.row.status === 1?'warning':'primary'"
                plain
                @click="changeStatus(scope.row.id,scope.row.status === 1?0:1)"
              >{{ scope.row.status === 1?'禁用':'正常' }}</el-button>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column
          prop="updateTime"
          label="修改时间"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column
          prop="address"
          label="操作"
          align="center"
          width="250"
        >
          <template slot-scope="scope">
            <el-button
              idsize="medium"
              type="primary"
              @click="openEdit(scope.row.id)"
            >编辑</el-button>
            <el-button
              idsize="medium"
              type="danger"
              @click="deleteRole(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="float: right;">
        <el-pagination
          background
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <edit-dialog ref="editDialog" @refreshData="getDataList()" />
  </div>
</template>
<script>
import EditDialog from './components/edit-dialog'
export default {
  components: {
    EditDialog
  },
  data() {
    return {
      queryParams: {
        roleName: '',
        status: null,
        currentPage: 1,
        pageSize: 10
      },
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  created() {
    this.getDataList()
  },
  methods: {
    /**
     * 获取角色数据
     */
    getDataList() {
      this.httpRequest({
        url: '/sys/role/list',
        method: 'get',
        params: this.queryParams
      }).then((response) => {
        if (response.code === 0) {
          this.tableData = response.body.records
          this.total = response.body.total
        } else {
          this.$message.error(response.msg)
        }
      })
    },
    /**
     * 删除角色
     */
    deleteRole(roleId) {
      this.$confirm('确认删除该角色吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.httpRequest({
          url: '/sys/role/delete/' + roleId,
          method: 'post'
        }).then((response) => {
          if (response.code === 0) {
            this.$message.success('删除成功')
            this.getDataList()
          } else {
            this.$message.error(response.msg)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    /**
     * 角色状态改变
     */
    changeStatus(roleId, status) {
      let hint = ''
      let successHint = ''
      if (status === 0) {
        hint = '确认启用该角色吗？'
        successHint = '启用成功'
      } else {
        hint = '确认禁用该角色吗？'
        successHint = '禁用成功'
      }
      this.$confirm(hint, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.httpRequest({
          url: '/sys/role/changeStatus',
          method: 'post',
          data: {
            roleId: roleId,
            status: status
          }
        }).then((response) => {
          if (response.code === 0) {
            this.$message.success(successHint)
            this.getDataList()
          } else {
            this.$message.error(response.msg)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    /**
     *打开编辑页
     */
    openEdit(id) {
      this.$refs.editDialog.init(id)
    },
    /**
     *打开编辑页
     */
    handleAdd() {
      this.$refs.editDialog.init()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.queryParams.pageSize = val
      this.getDataList()
    },
    handleCurrentChange(val) {
      this.queryParams.currentPage = val
      this.currentPage = val
      this.getDataList()
    }
  }
}
</script>
<style type="text/css">

</style>
